﻿using DocumentFormat.OpenXml.Wordprocessing;
using Docx2Epub.Docx;
using Docx2Epub.Epub;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Docx2Epub
{
    public partial class MainForm : Form
    {
        private DocDocument document;

        public MainForm()
        {
            InitializeComponent();
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            using (var ofd = new OpenFileDialog())
            {
                ofd.Filter = "Docx Files (.docx)|*.docx";
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    document = new DocDocument(ofd.FileName);
                    document.Analyze();
                    foreach (DocElement elm in document.Elements)
                        if (elm.ToString().Length > 20)
                            this.result.AppendText(elm.ToString().Substring(0, 20) + "..." + Environment.NewLine);
                        else
                            this.result.AppendText(elm + Environment.NewLine);
                }
            }
        }

        private void outputButton_Click(object sender, EventArgs e)
        {
            // TODO test data
            EPubMetadatas metadatas = new EPubMetadatas();
            metadatas.BookId = "urn:uuid:" + Guid.NewGuid().ToString();
            metadatas.Contributor = this.contributerBox.Text;
            metadatas.Creator = this.creatorBox.Text;
            metadatas.Language = "ja";
            metadatas.ModifiedDate = DateTime.Today;
            metadatas.PublishDate = DateTime.Today;
            metadatas.Title = this.titleBox.Text;
            metadatas.Publisher = this.publisherBox.Text;
            metadatas.Rights = string.Empty;
            metadatas.CoverImagePath = this.coverPathBox.Text;
            metadatas.TemplateDirectory = @"Templates\default";

            using (var fbd = new FolderBrowserDialog())
            {
                fbd.Description = "EPUBファイル一式出力先フォルダを指定してください";
                if (fbd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    return;

                var writer = new EpubBuilder(this.document.Elements, metadatas);
                writer.WriteOut(fbd.SelectedPath);
            }

        }

        private void referCoverPath_Click(object sender, EventArgs e)
        {
            using (var ofd = new OpenFileDialog())
            {
                ofd.Filter = "Image Files|*.jpg; *.png";
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.coverPathBox.Text = ofd.FileName;
                }
            }
        }

        private void packageButton_Click(object sender, EventArgs e)
        {

            using (var fbd = new FolderBrowserDialog())
            {
                fbd.Description = "パッケージ対象のフォルダを選択してください";
                if (fbd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    return;
                using (var sfd = new SaveFileDialog())
                {
                    sfd.Title = "保存先ファイル名を指定してください";
                    sfd.Filter = "EPUB Files (.epub)|*.epub";
                    if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                        return;

                    new EpubPackager(fbd.SelectedPath, sfd.FileName).WriteOut();
                }
            }
        }

        private void licenseInfo_Click(object sender, EventArgs e)
        {
            new LicenseForm().ShowDialog();
        }

    }
}
